package user_heart

import (
	"gin-base/base/resp"
	"gin-base/common/middleware/token"
	"gin-base/common/models/userHeart"
	"github.com/gin-gonic/gin"
	"strconv"
)

type CreateForm struct {
	Max  string `form:"Max" json:"Max" binding:"required"`
	Data string `form:"Data" json:"Data" binding:"required"`
}

func Create(this *gin.Context) {
	var err error
	var form CreateForm
	user := token.User(this)

	bindErr := this.BindJSON(&form)
	if bindErr == nil {
		var one = userHeart.GetOneByUserId(user.ID)
		one.UserId = user.ID
		one.Max, err = strconv.ParseFloat(form.Max, 64)
		one.Data = form.Data

		if err != nil {
			resp.Error(this, "PARAM", err.Error())
		}

		if one.Save() != nil {
			resp.Error(this, "PARAM", err.Error())
		}

		resp.Success(this, userHeart.GetOneByUserId(user.ID))
	}
}

func Last(this *gin.Context) {
	user := token.User(this)
	userHeart := userHeart.GetOneByUserId(user.ID)
	if userHeart.ID == 0 {
		resp.Success(this, nil)
	} else {
		resp.Success(this, userHeart)
	}
}
